package com.yy.job.console.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yy.job.common.dao.YYJobDOMapper;
import com.yy.job.common.dao.po.YYJobDO;
import com.yy.job.common.dao.po.YYJobLogDO;
import com.yy.job.common.enums.JobActionEnum;
import com.yy.job.common.enums.JobStatusEnum;
import com.yy.job.common.service.impl.YYJobLogServiceImpl;

@Service
public class JobConsoleServicve {
	
	@Autowired
	private YYJobDOMapper yyJobDOMapper;
	
	@Autowired
	private YYJobLogServiceImpl yyJobLogServiceImpl;
	
	/**
	 * 批量关闭
	 * 
	 * @param ids
	 * @param modifier
	 * @return
	 */
	@Transactional
	public Integer batchCloseJob(List<Long> ids, String modifier) {
		if(CollectionUtils.isEmpty(ids)) {
			return 0;
		}
		
		int totalCount = 0;
		for(Long id : ids) {
			YYJobDO selectByPrimaryKey = this.yyJobDOMapper.selectByPrimaryKey(id);
			// 只有为FAILE的任务可以关闭
			if(selectByPrimaryKey == null || !JobStatusEnum.FAIL.getCode().equals(selectByPrimaryKey.getJobStatus())) {
				continue;
			}
			
			// 将状态更新为success
			YYJobDO row = new YYJobDO();
			row.setId(id);
			row.setJobStatus(JobStatusEnum.CLOSE.getCode());
			row.setModifier(modifier);
			row.setGmtModified(new Date());
			int updateByPrimaryKeySelective = yyJobDOMapper.updateByPrimaryKeySelective(row);
			
			// 增加一条日志
			YYJobLogDO jobLogDO = new YYJobLogDO();
			jobLogDO.setJobId(id);
			jobLogDO.setJobAction(JobActionEnum.CLOSE.getCode());
			jobLogDO.setManualExecUser(modifier);
			jobLogDO.setExecStartTime(new Date());
			jobLogDO.setCreator(modifier);
			jobLogDO.setModifier(modifier);
			yyJobLogServiceImpl.saveSelective(jobLogDO);
			
			totalCount = totalCount + updateByPrimaryKeySelective;
		}
		
		return totalCount;
	}
	
	/**
	 * 批量重启
	 * 
	 * @param ids
	 * @param modifier
	 * @return
	 */
	@Transactional
	public Integer batchResetJob(List<Long> ids, String modifier) {
		if(CollectionUtils.isEmpty(ids)) {
			return 0;
		}
		
		int totalCount = 0;
		for(Long id : ids) {
			YYJobDO selectByPrimaryKey = this.yyJobDOMapper.selectByPrimaryKey(id);
			// 只有为FAIL的任务可以重启
			if(selectByPrimaryKey == null || !JobStatusEnum.FAIL.getCode().equals(selectByPrimaryKey.getJobStatus())) {
				continue;
			}
			
			// 将状态更新为create
			YYJobDO row = new YYJobDO();
			row.setId(id);
			row.setJobStatus(JobStatusEnum.CREATE.getCode());
			row.setExecCount(0);
			row.setModifier(modifier);
			row.setGmtModified(new Date());
			int updateByPrimaryKeySelective = yyJobDOMapper.updateByPrimaryKeySelective(row);
			
			// 增加一条日志
			YYJobLogDO jobLogDO = new YYJobLogDO();
			jobLogDO.setJobId(id);
			jobLogDO.setJobAction(JobActionEnum.MANU.getCode());
			jobLogDO.setManualExecUser(modifier);
			jobLogDO.setExecStartTime(new Date());
			jobLogDO.setCreator(modifier);
			jobLogDO.setModifier(modifier);
			yyJobLogServiceImpl.saveSelective(jobLogDO);
			
			totalCount = totalCount + updateByPrimaryKeySelective;
		}
		
		return totalCount;
	}
	
}
